//
//  MoveDetailTableViewCell.m
//  练习06
//
//  Created by hcios on 2017/7/7.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "MoveDetailTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UILabel+YBAttributeTextTapAction.h"

#define FONT_NAME_LTH                   @"FZLanTingHeiS-R-GB"
#define RGBA(r,g,b,a) [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)


@implementation MoveDetailTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    
}

#pragma mark - 初始化方法
+(instancetype)cellWithTableView:(UITableView *)tableView{
    //cell复用，唯一标识
    static NSString *identifier = @"CellID";
    //先在缓存池中取
    MoveDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //缓存池中没有再创建，并添加标识，cell移出屏幕时放入缓存池以复用
    if (cell == nil) {
        cell = [[MoveDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
    cell.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10);
    
    return cell;
}


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //取消高亮状态
//        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupSubViews];
        
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clicks:)];
        self.cImageview.userInteractionEnabled = YES;
        [self.cImageview addGestureRecognizer:ges];
    }
    return self;
}

-(void)clicks:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clicks:)]) {
        [self.delegate clicks:tap];
    }
}

#pragma mark - 视图初始化
//视图初始化
-(void)setupSubViews{
    
    //cImageview
    self.cImageview = [[UIImageView alloc] init];
    self.cImageview.layer.cornerRadius = 4;
    self.cImageview.layer.masksToBounds = YES;
    [self addSubview:self.cImageview];
    
    
    //user_name
    self.user_name = [[UILabel alloc] init];
    [self.user_name setFont:[UIFont fontWithName:FONT_NAME_LTH size:13]];
    [self.user_name setTextColor:[UIColor whiteColor]];
    self.user_name.shadowColor = [UIColor grayColor];
    self.user_name.shadowOffset = CGSizeMake(1, 1);
    [self.cImageview addSubview:self.user_name];
    
    
    //trip_name
    self.trip_name = [[UILabel alloc] init];
    [self.trip_name setTextColor:[UIColor whiteColor]];
    [self.trip_name setFont:[UIFont fontWithName:FONT_NAME_LTH size:13]];
    self.trip_name.shadowColor = [UIColor grayColor];
    self.trip_name.shadowOffset = CGSizeMake(1, 1);
    [self.cImageview addSubview:self.trip_name];
    
    
    //contextLab
    self.contextLab = [[UILabel alloc] init];
    [self.contextLab setFont:[UIFont fontWithName:FONT_NAME_LTH size:13]];
    [self.contextLab setTextColor:[UIColor blackColor]];
    [self addSubview:self.contextLab];
    
    
    //titleLab
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.numberOfLines = 0;
    [self.titleLab setFont:[UIFont fontWithName:FONT_NAME_LTH size:15]];
    [self.titleLab setTextColor:[UIColor blackColor]];
    [self addSubview:self.titleLab];
    
    
    //setTitleView
    self.setTitleView = [[UIView alloc] init];
    self.setTitleView.backgroundColor = [UIColor cyanColor];
    [self addSubview:self.setTitleView];
    
    
    //底部的地标
    self.buttonLabel = [[UILabel alloc] init];
    [self.buttonLabel setFont:[UIFont fontWithName:FONT_NAME_LTH size:12]];
    self.buttonLabel.textColor = RGB(30, 144, 255);
    self.userInteractionEnabled = YES;
    self.buttonLabel.userInteractionEnabled = YES;
    [self addSubview:self.buttonLabel];
    
    //手势
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesButton:)];
    [self.buttonLabel addGestureRecognizer:ges];
}

//添加手势
-(void)gesButton:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickButtonLabel:)]) {
        [self.delegate clickButtonLabel:tap];
    }
}


#pragma mark - 重写set方法
//重写set方法
-(void)setFrameModel:(MoveDetailFrameModel *)frameModel{
    _frameModel = frameModel;
    MoveDetailModel *model = frameModel.model;
    
    [self.cImageview sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:nil];
    self.cImageview.frame = frameModel.imageFrame;
    
    if (![model.note_user_name isEqualToString:@""]) {
        self.user_name.text = [NSString stringWithFormat:@"@%@",model.note_user_name];
    
        self.user_name.frame = frameModel.user_nameFrame;
    }
    self.trip_name.text = model.note_trip_name;
    self.trip_name.frame = frameModel.trip_nameFrame;
    
    self.titleLab.text = model.title;
    self.titleLab.numberOfLines = 0;
    self.titleLab.frame = frameModel.titleFrame;
    
    if ([model.note_user_name isEqualToString:@""]) {
        self.contextLab.frame = CGRectZero;
    }else{
        self.contextLab.numberOfLines = 0;
        self.contextLab.text = model.description_text;
        self.contextLab.frame = frameModel.contextFrame;
    }
    
    if ([model.title isEqualToString:@""]) {
        self.setTitleView.frame = CGRectZero;
    }else self.setTitleView.frame = frameModel.setTitleViewFrame;
    
    self.buttonLabel.text = model.name;
    self.buttonLabel.frame = frameModel.buttonLabelFrame;
}


@end
